package com.psa.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.psa.queryelement.QueryElement;
import com.psa.system.R;
import com.wxw.BaseLibrary.component.DataGridView;
import com.wxw.BaseLibrary.element.JsonProperty;

/**
 * Created by david on 2017-10-24.
 */
public class OrderExpandableListAdapter extends BaseExpandableListAdapter {
    private QueryElement queryElement = null;
    private Context context = null;
    private LayoutInflater inflater = null;
    private ButtonClickListener buttonClickListener = null;

    public OrderExpandableListAdapter(Context context, QueryElement data, ButtonClickListener listener) {
        this.context = context;
        inflater = LayoutInflater.from(this.context);
        queryElement = data;
        buttonClickListener = listener;
    }

    @Override
    public int getGroupCount() {
        return queryElement.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return 1;
    }

    @Override
    public Object getGroup(int i) {
        return queryElement.getItems().get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        JsonProperty jp = queryElement.getItems().get(i);
        return jp.getProperty("items")!=null?jp.getProperty("items").getItems().get(i1):null;
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        OrderExpandableListAdapter.GroupViewHolder holder = null;
        JsonProperty jsonProperty = queryElement.getItems().get(i);
        if (view == null) {
            view = inflater.inflate(R.layout.order_exlistview_item_layout, null);
            holder = new OrderExpandableListAdapter.GroupViewHolder();
            holder.textView = (TextView)view.findViewById(R.id.textView);
            holder.btn_pay = (Button)view.findViewById(R.id.btn_pay);
            view.setTag(holder);
        } else {
            holder = (OrderExpandableListAdapter.GroupViewHolder)view.getTag();
        }
        String s = "";
        for (int j=0;j<queryElement.getColumnSize();j++) {
            s += queryElement.getColumnFloatFormat(j)+ jsonProperty.getProperty(queryElement.getColumnName(j)).getValue() + "\n";
        }
        Drawable drawable = context.getResources().getDrawable((b?R.drawable.dropup:R.drawable.dropdown));
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        holder.textView.setText(s.substring(0, s.length()-1));
        holder.textView.setCompoundDrawables(null, null, drawable, null);
        holder.btn_pay.setOnClickListener(buttonClickListener);
        holder.btn_pay.setTag(i);
        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        OrderExpandableListAdapter.ChildViewHolder holder = null;
        JsonProperty jsonProperty = queryElement.getItems().get(i).getProperty("items");
        if (view == null) {
            view = inflater.inflate(R.layout.exlistview_child_item_layout, null);
            holder = new OrderExpandableListAdapter.ChildViewHolder();
            holder.dataGridView = new DataGridView(context);
            LinearLayout linearLayout = (LinearLayout)view.findViewById(R.id.ll_dataGrid);
            linearLayout.addView(holder.dataGridView);
            view.setTag(holder);
        } else {
            holder = (OrderExpandableListAdapter.ChildViewHolder)view.getTag();
        }
        try {
            QueryElement qe = new QueryElement(queryElement.getSetting().toString(), jsonProperty.toString(), true);
            holder.dataGridView.fullGridView(qe);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    class GroupViewHolder {
        public TextView textView;
        public Button btn_pay;
    }

    class ChildViewHolder {
        public DataGridView dataGridView;
    }

    public abstract static class ButtonClickListener implements View.OnClickListener {
        /**
         * Called when a view has been clicked.
         *
         * @param v The view that was clicked.
         */
        @Override
        public void onClick(View v) {
            click((Integer)v.getTag(), v);
        }

        public abstract void click(int position, View v);
    }
}
